$(function() {  
    createClose();
	var search = $('#search');
	var create = $('#create');
	var content = $('#content');
	var caches = [];
	var localData = meetCache.getMeetStorage();
	var chinese = /^[\u4E00-\u9FFF]+$/;
//	var callerHtml='<dl name="meet" id="sessionid"><dt><div class="call_pepople_timecstatus"><b>names</b><span>times</span></div><div class="call_tip_divide">多方通话<i>（count人）</i><em><img src="images/go.png" width="14" height="13"></em></div></dt><dd name="detailsMeet"  id="sessionid"><img src="images/detail.png" width="22" height="22"></dd></dl>';
	var callerHtml='<dl name="meet" id="sessionid"><dt><div class="call_pepople_timecstatus"><b>names</b></div><div class="call_tip_divide"><div class="call_left">多方通话<i>（count人）</i><em><img src="images/come.png" width="14" height="13"></em></div><span>times</span></div></dt><dd name="detailsMeet"  id="sessionid"><img src="images/detail.png" width="22" height="22"></dd></dl>';
	var dialHtml='<dl name="meet" id="sessionid"><dt><div class="call_pepople_timecstatus"><b>names</b></div><div class="call_tip_divide"><div class="call_left">两方通话<i>（count人）</i><em><img src="images/come.png" width="14" height="13"></em></div><span>times</span></div></dt><dd name="detailsMeet"  id="sessionid"><img src="images/detail.png" width="22" height="22"></dd></dl>';
	var voiceHtml='<dl name="meet" id="sessionid"><dt><div class="call_pepople_timecstatus"><b>names</b></div><div class="call_tip_divide"><div class="call_left">电话留言<i>（count人）</i></div><span>times</span></div></dt><dd name="detailsMeet" id="sessionid"><img src="images/detail.png" width="22" height="22"></dd></dl>';
//	var voiceHtml='<dl name="meet" id="sessionid"><dt><div class="call_pepople_timecstatus"><b>names</b><span>times</span></div><div class="call_tip_divide">电话留言<i>（count人）</i></div></dt><dd name="detailsMeet" id="sessionid"><img src="images/detail.png" width="22" height="22"></dd></dl>';
	var htmlRespority={
	  'VoiceNotice3':voiceHtml,
	  'TalksV2':callerHtml,
	  'Dial':dialHtml
	}
	var htmlText;
	$('#content').height(
			getHeight($(document), [ $('#headerContent'), $('#searchContent'),
					$('#create') ])).css('overflow-y','auto');

	function Contact(id, name, tel, time, status, html) {
		this.id = id;
		this.name = name;
		this.tel = tel;
		this.html = html;
		this.time = time;
		this.phonetic = [];
		this.flphonetic = [];
		this.status = status;
		for (var m = 0; m < this.name.length; m++) {
			var nm = this.name[m];
			if (chinese.test(nm)) {
				var r = cc2phonetic(nm);
				if (r && r.length == 2) {
					this.phonetic.push(r[0]);
					this.flphonetic.push(r[1]);
				}
			}
		}
	}

	Contact.prototype.match = function(value) {
		return this.name.join(',').indexOf(value) > -1
				|| this.tel.join(',').indexOf(value) > -1
				|| this.phonetic.join(',').indexOf(value) > -1
				|| this.flphonetic.join(',').indexOf(value) > -1;
	};

	function init(elem, callback) {
		if (elem && elem.data) {
			var arr = elem.data;
			var phones = [];
			var names = [];
			for (var i = 0; i < arr.length; i++) {
				phones.push(arr[i].telephone);
				names.push(arr[i].name);
			}
			caches.push(callback(new Contact(elem.session_id, names, phones,
					elem.time, elem.status),elem.type));
		}
	}

	(function() {
		if (localData) {
			var htmls = [];
			for (var i = 0; i < localData.length; i++) {
				var data = localData[i];
				init(
						data,
						function(contact,type) {
							var html = htmlRespority[type]
									.replace(
											/(names|count|times|sessionid|cstatus)/g,
											function(name) {
												if (name == 'names'){
													if(type==='VoiceNotice3')
														return contact.name.slice(1).join('、');
													else
													    return contact.name.join('、');
												}
												else if (name == 'count') {
													if(type==='VoiceNotice3')
														return contact.name.length-1;  
													else
													    return contact.name.length;
												} else if (name == 'times') {
													return formatDateTime(contact.time);
												} else if (name == 'sessionid') {
													return contact.id;
												} else if (name == 'cstatus') {
													return (contact.status == 'fail') ? ' access_fail'
															: '';
												} else
													return name;
											});
							htmls.push(html);
							contact.html = html;
							return contact;
						});
			}
			content.html((htmlText = htmls.join('')));
		}
	})();

	create.on('click', function() {
		createMeeting('Launch_meeting.html');
	});
    
    $('#voice').on('click', function() {
        createMeeting('text_recording.html');
    }); 
    
	$(document.body).on('click',"dd[name='detailsMeet']",  
			function(event) {
				event.stopPropagation();
				var target = $(this);
				var id = target.attr('id');
				for (var i = 0; i < localData.length; i++) {					
					if (localData[i].session_id == id) {
						if(localData[i].type === 'VoiceNotice3'){ //跳转至语音通知详情页
							var key="key"+localData[i].session_id;
							localCache.setSessionItem(key,JSON.stringify(localData[i]));
							location.assign('yy_message_detail.html?sms='+key);  
						} else if(localData[i].type === 'Dial'){//两方通话页面
							var caller="",called="";
							if(localData[i].data){
								var data=localData[i].data;
								for (var k = 0; k < data.length; k++) {
									if(data[k].type==='main'){
										caller=data[k].telephone;
									}else
										called=data[k].telephone;  
								}
							}
							location.assign(encodeURI('two_call_details.html?data='
									+ JSON.stringify({
										"phoneNum":caller,
										"called":called
									})));
						} else { //跳转至多方通话详情页
							location.assign(encodeURI('call_details.html?meet='
									+ JSON.stringify(localData[i])));
						}
					}
				}
		});
	
	$(document.body).on('click', "dl[name='meet']",function(event) { 
	    event.stopPropagation();
	    var id = $(this).attr('id');
	    var param;
	    if (id && localData) {
	      for (var i = 0; i < localData.length; i++) {
	        if (localData[i].session_id === id) {
	          param = {
	            data : localData[i].data
	          };    
			  if(localData[i].type === 'VoiceNotice3'){ //跳转至语音通知详情页
				  param.tts = localData[i].tts;
		  	      createMeeting('text_recording.html', param);
			  } else { //跳转至多方通话详情页
		  	      createMeeting('Launch_meeting.html', param);
			  }
	          break;
	        }
	      }
	    }
	  });

	search.on('input', function() {
		var value = this.value;
		if (value) {
			var rc = [];
			for (var i = 0; i < caches.length; i++) {
				var data = caches[i];
				if (data.match(this.value)) {
					rc.push(data.html);
				}
			}
			content.html(rc.join(''));
		} else {
			content.html(htmlText);
		}
	});

})
